package src

import (
	"fmt"
	"sync"
)

type Worker struct {
}

func (w Worker) worker(task chan Task) {
	for t := range task {
		t.Run()
	}
}

type WorkManage struct {
	workers     []Worker
	workChannel chan Task
	wg          *sync.WaitGroup
}

// todo Push任务不应该阻塞
func (wm WorkManage) Push(t Task) {
	wm.workChannel <- t
}

func (wm *WorkManage) Init(n int, wg *sync.WaitGroup) {
	wm.workers = make([]Worker, n)
	wm.workChannel = make(chan Task, n*2)
	wm.wg = wg
}

// TODO 这里多个worker公用一个channel，考虑改成扇入扇出模式
func (wm WorkManage) Execute() {
	fmt.Println("len worker is ", len(wm.workers))
	for _, work := range wm.workers {
		go func(w Worker) {
			defer wm.wg.Done()
			w.worker(wm.workChannel)
		}(work)
	}
}
